package org.eclipse.assemblyformatter.ir;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;

public class Section {
	protected Position position;
	protected Section nextSection;
	private static Section[] nextIs__staticData;

	static {
		nextIs__staticData = new Section[2/*
										 * Maximum number of nextIs(...)
										 * parameters
										 */];
	}

	public Section() {
		this(0);
	}

	public Section(int offset) {
		position = new Position(offset);
	}

	public Position getPosition() {
		return position;
	}

	public int getOffset() {
		return position.getOffset();
	}

	public void setOffset(int offset) {
		position.setOffset(offset);
	}

	public void copyOffset(Section section) {
		this.position.setOffset(section.position.getOffset());
	}

	public int getLength() {
		return position.getLength();
	}

	public Section getNextSection() {
		return nextSection;
	}

	public void setNextSection(Section nextSection) {
		this.nextSection = nextSection;
		// if (this.nextSection != null) {
		// this.position.setLength(this.nextSection.position.getOffset()
		// - this.position.getOffset());
		// }
	}

	// public void appendToDocumentContentBuilder() {
	// String s = documentContent.substring(position.getOffset(),
	// position.getOffset() + length());
	// documentContentBuilder.append(s);
	// }

	/**
	 * This is how the data obtained with nextIs(...) can be retrieved.
	 * 
	 * @param index
	 * @return
	 */
	public static Section getNextIs__staticData(int index) {
		return nextIs__staticData[index];
	}

	/**
	 * 
	 * @param sectionClass
	 * @return
	 */
	public boolean nextIs(Class sectionClass) {
		if (nextSection != null) {
			if (sectionClass.isInstance(nextSection)) {
				nextIs__staticData[0] = nextSection;
				return true;
			}
		}
		return false;
	}

	public boolean nextIs(Class sectionClass, Class nextSectionClass) {
		if (nextSection != null) {
			if (sectionClass.isInstance(nextSection)) {
				nextIs__staticData[0] = nextSection;
				if (nextSection.nextSection != null) {
					if (nextSectionClass.isInstance(nextSection.nextSection)) {
						nextIs__staticData[1] = nextSection.nextSection;
						return true;
					}
				}
			}
		}
		return false;
	}

	public String getContent(IDocument doc) throws BadLocationException {
		return doc.get(position.getOffset(), position.getLength());
	}

	public void setLength(int length) {
		position.setLength(length);
	}

	/**
	 * TODO Avoid this function
	 * 
	 * @param end
	 *            non-inclusive index
	 */
	public void setEnd(int end) {
		position.setLength(end - position.getOffset());
	}

	public void copyPosition(Section section) {
		this.position = section.position;
	}
}
